﻿// version 0.3 -> modificaciones en el isMouseOver() para definir cosas más complejas
// version 0.2 -> le agregue un if porque a veces el chabon se encargaba de choripanearse al parent
// 

class com.dmotiko.util.MovieClipUtils {

	private static var actualClip:MovieClip;
	
	public static function destroyClip(clip:MovieClip, child:Boolean):Void {
		
		if (!child) {
			actualClip = clip;
		}
		
		for(var sNested:String in clip){
			if(clip[sNested] != actualClip && clip[sNested] instanceof MovieClip) {
				//trace(clip[sNested]+" es un parent?"+ isParent(clip[sNested]));
				if(isChild(clip, clip[sNested])) destroyClip(clip[sNested], true);
			};
						
		};
		//if(!child) trace("\n---MovieClipUtils.as | destroyClip() end\n\n");
		removeMovieClip(clip);
		
	}; //fin funci
	
	//esta función es para hacer hijos en un clip de forma seriada, de forma progresiva
	public static function getSerialName(clip:MovieClip, name:String):String {
		var nCont:Number=0;
		while(clip["mc"+name+nCont]){
			nCont++;
		};
		return "mc"+name+nCont;	
	};
	
	public static function isMouseOver(clip:MovieClip, clipB:MovieClip):Boolean {
		var check:Boolean;
		if(!clip) { throw new Error("MovieClipUtils.as | isMouseOver() | falta definir el movieclip"); }
		if(!clipB){ var clipB:MovieClip = clip; }
		var bounds:Object = clip.getBounds(clipB);
		check = (clipB._xmouse > bounds.xMin && clipB._ymouse < bounds.xMax && clipB._ymouse > bounds.yMin && clipB._ymouse < bounds.yMax);
		return check;
	};
	
	public static function isParent(clip:MovieClip):Boolean {
		var parent:MovieClip = actualClip._parent;
		
		while(clip != parent && parent != _level0){
			parent = parent._parent;
		}
		if(parent == _level0){
			 return false;
		} else {
			 return true;
		};
	};
	
	public static function isChild(source:MovieClip, clip:MovieClip):Boolean {
		var check:Boolean = eval(targetPath(source) + "." + clip._name) == clip;
		//trace("MovieClipUtils.as | "+clip+" isChildof "+source+": "+ check);
		return check;
	};
	
}; // fin de clase